 <template>
  <div>
    <div>
      <p-form :labelCol="{ span: 6 }" :wrapperCol="{ span: 12 }" style="margin-top:30px">
        <p-row :guter="5">
          <p-col :span="7">
            <p-form-item label="车间">
              <p-select placeholder="请选择" v-model="workrooms.textValue">
                <!-- 遍历下拉选择框选项 -->
                <p-select-option
                @click="handelWorkrooms(item)"
                  :value="item.textValue"
                  v-for="(item,index) in workrooms2"
                  :key="index"
                >{{item.textValue}}</p-select-option>
              </p-select>
            </p-form-item>
          </p-col>
          <p-col :span="7">
            <p-form-item label="二级工序">
              <p-select placeholder="请选择" v-model="processTwos.textValue">
                <!-- 遍历下拉选择框选项 -->
                <p-select-option
                @click="handleProcessTwos(item)"
                  :value="item.textValue"
                  v-for="(item,index) in processTwos2"
                  :key="index"
                >{{item.textValue}}</p-select-option>
              </p-select>
            </p-form-item>
          </p-col>
          <p-col :span="7">
            <p-form-item label="线体">
              <p-select placeholder="请选择" v-model="lines.textValue">
                <!-- 遍历下拉选择框选项 -->
                <p-select-option
                  @click="handleLines(item)"
                  :value="item.textValue"
                  v-for="(item,index) in lines2"
                  :key="index"
                >{{item.textValue}}</p-select-option>
              </p-select>
            </p-form-item>
          </p-col>
          <p-col :span="3">
            <p-button type="primary" @click="handleScan">查询</p-button>
            <p-button type="primary" @click="searchReset" style="margin-left:20px">重置</p-button>
          </p-col>
        </p-row>
      </p-form>
    </div>
    <!-- 工序工单调整 -->
    <p-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :scroll="tableScroll"
      :dataSource="dataSource"
      :loading="loading"
      :pagination="ipagination"
      style="margin-top:15px"
      :page-size-options="pageSizeOptions"
      @change="handleTableChange"
    >

      <template slot="priority" slot-scope="text,record">
        <p-input-number
          v-model="record.priority"
          :min="1"
          :max="9"
          @change="changPriority(record)"
          :disabled="record.billnoStatus>=2?true:false"
        ></p-input-number>
      </template>
    </p-table>
  </div>
</template>  

<script>
import moment from "moment";
import { postAction, getAction, deleteAction } from "@/api/manage";
import { JeecgListMixin } from "@/mixins/JeecgListMixin";
import { filterObj } from "@/utils/util";
import Vue from "vue";

export default {
  name: "",
  mixins: [JeecgListMixin],
  components: {
    moment,
  },
  props: {
    rowInfo: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  mounted() {
    this.receiveRowInfo = this.rowInfo;
  },
  data() {
    return {
      rowData: {},
      resultData: {
        workroomno: "",
        processTwocode: "",
        lineno: "",
      },
      lines2: [],
      AllChange:[],
      processTwos2: [],
      workrooms2: [],
      workrooms:{ codeValue:"",textValue:''},
      processTwos:{ codeValue:"",textValue:''},
      lines:{ codeValue:"",textValue:''},
      receiveRowInfo: {},
      //时间
      moment,
      EnhanceJS: "",
      cgButtonLinkList: [],
      cgButtonList: [],
      title: "", //存表名
      groupTag: [],
      queryInfo: [],
      code: "",
      // 表头
      columns: [],
      //列设置
      settingColumns: [],
      //列定义
      defColumns: [
        {
          title: "序号",
          dataIndex: "id",
          key: "rowIndex",
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: "工单号",
          dataIndex: "processbillno",
          key: "processbillno",
          // width: "15%",
          align: "center",
          scopedSlots: { customRender: "encodingRules" },
        },
        {
          title: "产品编码",
          dataIndex: "productcode",
          key: "productcode",
          // width: "40%",
          align: "center",
          scopedSlots: { customRender: "productcode" },
        },
        {
          title: "产品型号",
          dataIndex: "productsprc",
          key: "productsprc",
          // width: "40%",
          align: "center",
          scopedSlots: { customRender: "productsprc" },
        },

        {
          title: "二级工序",
          dataIndex: "processTwoname",
          key: "processTwoname",
          align: "center",
          scopedSlots: { customRender: "processTwoname" },
        },
        {
          title: "线体",
          dataIndex: "linename",
          key: "linename",
          align: "center",
          scopedSlots: { customRender: "linename" },
        },
        {
          title: "计划开始时间",
          dataIndex: "starttime",
          key: "starttime",
          align: "center",
          customRender: (text) => {
            return moment(text).format("YYYY-MM-DD");
          },
        },
        {
          title: "计划结束时间",
          dataIndex: "completiontime",
          key: "completiontime",
          align: "center",
          customRender: (text) => {
            return moment(text).format("YYYY-MM-DD");
          },
        },
        {
          title: "优先级",
          dataIndex: "priority",
          key: "priority",
          align: "center",
          scopedSlots: { customRender: "priority" },
          defaultSortOrder: "descend",
          sorter: (a, b) => a.priority - b.priority,
        },
        {
          title: "工单状态",
          dataIndex: "billnoStatus",
          key: "billnoStatus",
          align: "center",
          customRender: (text) => {
            if (text === -1) {
              return "已删除";
            } else if (text === 0) {
              return "未发布";
            } else if (text === 1) {
              return "已发布";
            } else if (text === 2) {
              return "已开工";
            } else if (text === 3) {
              return "已暂停";
            }
          },
        },
      ],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ["5", "10", "20"],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条";
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      url: {
        //获取查询条件
        scan: "/mes-manufacture/mfProcess/getFilter",
        //分页获取工单调整列表
        list1: "/mes-manufacture/mfProcess/adjustment",
      },
    };
  },
  created() {
    this.initColumns();
    this.setdefColumns();
    this.scanData();
  },
  methods: {
    //改变优先级
    changPriority(val) {
      let obj={
          id:val.id,
          priority:val.priority
      }
      this.AllChange.push(obj)
      console.log(val)
       this.$emit("sentDataIfo", this.AllChange);
    },
    handelWorkrooms(val1) {
      console .log(val1)
      this.workrooms = val1;
    },
    handleProcessTwos(val2) {
      this.processTwos = val2;
    },
    handleLines(val3) {
      this.lines = val3;
    },
    handleScan() {
      console.log(this.processTwos.codeValue)
      this.loadData();
    },
  
    tableScroll: function () {
      if (this.columns.length > 13) {
        return { x: this.columns.length * 150 - 450 };
      }
    },
    setdefColumns() {
      if (this.columns.length > 13) {
        let arrLength = this.columns.length - 1;
        this.columns.forEach((item, index) => {
          item["width"] = 130;
        });
        this.columns[arrLength]["fixed"] = "right";
        this.columns[0]["fixed"] = "left";
        this.columns[1]["fixed"] = "left";
      }
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ":colsettings";
      let colSettings = Vue.ls.get(key);
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = [];
        this.defColumns.forEach(function (item, i, array) {
          allSettingColumns.push(item.dataIndex);
        });
        this.settingColumns = allSettingColumns;
        this.columns = this.defColumns;
      } else {
        this.settingColumns = colSettings;
        const cols = this.defColumns.filter((item) => {
          if (item.key == "rowIndex" || item.dataIndex == "action") {
            return true;
          }
          if (colSettings.includes(item.dataIndex)) {
            return true;
          }
          return false;
        });
        this.columns = cols;
      }
    },

    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ":colsettings";
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000);
      this.settingColumns = checkedValues;
      const cols = this.defColumns.filter((item) => {
        if (item.key == "rowIndex" || item.dataIndex == "action") {
          return true;
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true;
        }
        return false;
      });
      this.columns = cols;
    },
    onClearSelected() {
      this.selectedRowKeys = [];
      this.selectionRows = [];
      this.selectedMainId = "";
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.rowData = selectionRows[0];
      console.log(this.rowData);
      this.selectedMainId = selectedRowKeys[0];
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    loadData(arg) {
      if (!this.url.list1) {
        this.$message.error("请设置url.list1属性!");
        return;
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      this.onClearSelected();
      var params = this.getQueryParams(); //查询条件
      this.loading = true;

      getAction(this.url.list1, params).then((res) => {
        if (res.success) {
          this.dataSource = res.data.records;
          this.dataSource.map((item) => {
            if (item.billnoStatus == 1) {
              this.$set(item, "boole", true);
            } else {
              this.$set(item, "boole", false);
            }
          });
          console.log(this.dataSource);
          this.ipagination.total = res.data.records.total;
        }
        if (res.code === 510) {
          this.$message.warning(res.msg);
        }
        this.loading = false;
      });
    },
    getQueryParams() {
      // 获取查询条件
      const sqp = {};
      if (this.superQueryParams) {
        sqp["superQueryParams"] = encodeURI(this.superQueryParams);
      }

      var param = Object.assign(
        sqp,
        this.queryParam,
        this.isorter,
        this.filters
      );
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      param.workroomno = this.workrooms.codeValue;
      param.processTwocode = this.processTwos.codeValue;
      param.lineno = this.lines.codeValue;
      return filterObj(param);
    },
    //获取查询条件
    scanData() {
      let startDate = this.moment(new Date()).format("YYYY-MM-DD 00:00:00");
      // console.log(startDate)
      let endDate = this.moment(new Date().getTime()).format(
        "YYYY-MM-DD 23:59:59"
      );

      getAction(this.url.scan, { startDate, endDate }).then((res) => {
        if (res.success) {
          console.log(res)
          const workrooms1 = [];
          res.data.workrooms.forEach((item1) => {
            if (item1 != null) {
              workrooms1.push(item1);
            }
            this.workrooms2 = workrooms1;
            console.log(this.workrooms2);
          });
          const processTwos1 = [];
          res.data.processTwos.forEach((item2) => {
            if (item2 !== null) {
              processTwos1.push(item2);
            }
            this.processTwos2 = processTwos1;
            // console.log(this.processTwos2);
          });
          const line1 = [];
          res.data.lines.forEach((item3) => {
            if (item3 !== null) {
              line1.push(item3);
            }
            this.lines2 = line1;
            // console.log(this.lines2);
          });
        }
      });
    },
    // 重置
    searchReset(){
      this.workrooms={ codeValue:"",textValue:''},
      this.processTwos={ codeValue:"",textValue:''},
      this.lines={ codeValue:"",textValue:''},
      this.loadData()
    }
  },
};
</script>

<style scoped lang="less">
</style>

